<?php
namespace Resmodel;


use Resmodel\Factory\ResTableGatewayFactory;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Predicate\Like;
use Zend\Db\Sql\Predicate\Operator;
use Zend\Db\Sql\Select;
use Zend\InputFilter\InputFilter;

class DayDishTable extends ResAbstractTable
{

    /**
     * @param $id
     * @return DayDish
     */
    public function getDayDish($id){
       return $this->getById($id);
    }

    /**
     * @return InputFilter
     */
    public function getInputFilter()
    {
        $filter = new InputFilter();


        return $filter;
    }

    public function saveDayDish($day, $dishIdArr){
        $this->getTableGateway()->delete(array('day_of_week'=>$day));
        if($dishIdArr)
        foreach ($dishIdArr as $aDish) {
            $this->insert(array('day_of_week'=>$day, 'dish_id'=>$aDish));
        }

    }

    public function getAllDishInDay($day){
        $dishTable = ResTableGatewayFactory::getDishTable();
        $select = $dishTable->getDbSqlSelect();
        $select->join(array('dd'=>'day_dish'), 'dd.dish_id = dish.id', array())
        ->where(array('dd.day_of_week'=>$day));
        return $dishTable->executeSelectForArray($select);
    }

    public function getAllDishInDayByCat($day, $catId){
        $dishTable = ResTableGatewayFactory::getDishTable();
        $select = $dishTable->getDbSqlSelect();
        $select->join(array('dd'=>'day_dish'), 'dd.dish_id = dish.id', array())
            ->join(array('dc'=>'dish_category'), 'dc.dish_id = dish.id', array())
            ->join(array('dl'=>'dish_lang'), 'dl.dish_id = dish.id', array('name'=>'name'))
            ->where(array('dd.day_of_week'=>$day))
            ->where(array('dc.category_id'=>$catId, 'dc.is_primary'=>1))
            ->where(array('dl.lang'=>$dishTable->getLang()));
        return $dishTable->executeSelectForArray($select);
    }
}